package cn.xiaoming.car.server.account.dao.persist.repository.impl;

import cn.xiaoming.car.server.account.dao.persist.mapper.DeptMapper;
import cn.xiaoming.car.server.account.dao.persist.repository.IDeptRepository;
import cn.xiaoming.car.server.account.pojo.entity.Admin;
import cn.xiaoming.car.server.account.pojo.entity.Dept;
import cn.xiaoming.car.server.account.pojo.vo.AdminListItemVO;
import cn.xiaoming.car.server.account.pojo.vo.DeptListItemVO;
import cn.xiaoming.car.server.account.pojo.vo.DeptStandardVO;
import cn.xiaoming.car.server.common.pojo.vo.PageData;
import cn.xiaoming.car.server.common.util.PageInfoToPageDataConverter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class DeptRepositoryImpl implements IDeptRepository {
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public int insert(Dept dept) {
        log.debug("开始执行【插入部门】的数据访问，参数：{}", dept);
        return deptMapper.insert(dept);
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始执行【删除部门】的数据访问，参数：{}", id);
        return deptMapper.deleteById(id);
    }

    @Override
    public int updateById(Dept dept) {
        log.debug("开始执行【更新部门】的数据访问，参数：{}", dept);
        return deptMapper.updateById(dept);
    }

    @Override
    public int countByDeptName(String deptName) {
        log.debug("开始执行【根据用户名统计部门数据的数量】的数据访问，参数：{}", deptName);
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept_name", deptName);
        return deptMapper.selectCount(queryWrapper);
    }

    @Override
    public DeptStandardVO getStandardById(Long id) {
        log.debug("开始执行【根据ID查询用户详情】的数据访问，参数：{}", id);
        return deptMapper.getStandardById(id);
    }

    @Override
    public PageData<DeptListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询部门列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<DeptListItemVO> list = deptMapper.list();
        PageInfo<DeptListItemVO> pageInfo = new PageInfo<>(list);
        PageData<DeptListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
